package demo.controller;

import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import javax.ws.rs.GET;

/**
 * @Author: 朱媛媛
 * @Description：
 * @Date： Created in 下午 3:39 on 2018/3/14 0014.
 */
@RestController
public class TicketController {

    @Resource
    RestTemplate restTemplate;

    @Resource
    private LoadBalancerClient loadBalancerClient;

    @RequestMapping("/ribbon/{id}")
    @GET
    public ResponseEntity test(@PathVariable int id) {
        ServiceInstance serviceInstance = this.loadBalancerClient.choose("springboot-h2");
        System.out.println("===" + ":" + serviceInstance.getServiceId() + ":" + serviceInstance.getHost() + ":"
                + serviceInstance.getPort());// 打印当前调用服务的信息
        User u = this.restTemplate.getForObject("http://springboot-h2/" + id, User.class);
        System.out.println(u);
        return ResponseEntity.ok(u);
    }
}
